﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Top10ContentWords : IService
{
    private string _url;
    private string _html;
    private string[] _topTen;
    private List<WordCounter> _searchWords = new List<WordCounter>();

    /// <summary>
    /// Constructor of Top10Words class
    /// </summary>
    /// <param name="html">Text to be parsed</param>
    public Top10ContentWords()
    {
        this._html = "";
        _topTen = new string[10];
    }

    /// <summary>
    /// Gets the top ten words in order of occurrences
    /// </summary>
    /// <returns>String array of top ten words</returns>
    public string[] getTopTen(string url)
    {
        this._url = url;
        Web2StringService.Service w2s = new Web2StringService.Service();
        WordFilterService.ServiceClient wf = new WordFilterService.ServiceClient();
        this._html = wf.FilterButLeaveStopWords(w2s.GetWebContent(this._url));
        this.parse();
        return _topTen;
    }

    /// <summary>
    /// Parses the text and counts occurrences of each word
    /// </summary>
    private void parse()
    {
        // Split the HTML at the spaces
        string[] words = _html.Split(' ');
            
        // loop through each word
        foreach (string word in words)
        {
            // create new WordCounter for each word, add it to the List if it is not blank and not already in the list
            WordCounter wc = new WordCounter(word);
            if (word != "")
            {
                if (!_searchWords.Contains(wc))
                {
                    _searchWords.Add(wc);
                }
            }
        }

        // iterate through each word and count number of occurrences in the text
        foreach (WordCounter word in _searchWords)
        {
            string targetWord = " " + word.getName() + " ";
            int result = countWord(targetWord);
            word.setOccurrences(result);
        }
        // Sort the results by number of occurrences
        this._searchWords.Sort();

        // Add the top ten to the topTen array
        for (int i = 0; i < 10; i++)
        {
            this._topTen[i] = _searchWords[i].getName();
        }
    }

    /// <summary>
    /// Counts number of times a given string appears in the text
    /// </summary>
    /// <param name="word">Word to search for</param>
    /// <returns>Number of occurrences</returns>
    private int countWord(string word)
    {
        // Number of occurrences
        int occurrences = 0;
 
        int i = 0;
        while ((i = _html.IndexOf(word, i)) != -1)
        {
            // found word, so increment the occurrences
            i += word.Length;
            occurrences++;
        }
        return occurrences;
    }

}
